<?php
class PermissionsController extends AppController {

	var $name = 'Permissions';
	var $uses = array('Permission', 'Group');
	var $helpers = array('Html', 'Form' );

	function index() {
		$this->Permission->recursive = 0;
		$this->set('permissions', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid Permission.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('permission', $this->Permission->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			//$this->cleanUpFields();
			$this->Permission->create();
			if ($this->Permission->save($this->data)) {
				$this->Session->setFlash('The Permission has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Permission could not be saved. Please, try again.');
			}
		}
		$groups = $this->Permission->Group->find('list');
		$this->set(compact('groups'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Permission');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if (!empty($this->data)) {
			//$this->cleanUpFields();
			if ($this->Permission->save($this->data)) {
				$this->Session->setFlash('The Permission has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Permission could not be saved. Please, try again.');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Permission->read(null, $id);
		}
		$groups = $this->Permission->Group->find('list');
		$this->set(compact('groups'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Permission');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if ($this->Permission->del($id)) {
			$this->Session->setFlash('Permission #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}

}
?>